<?php
namespace app\action\admin;
use core\BaseAction;
use core\App;
use core\JsonInfo;
use core\ParamFilter;
use stdclass;
class UserAction extends BaseAction{
	function getList($params,$req,$res){
		return $this->view("admin.user_list");
	}
	
	function postList($params){
		//初始化查询过滤器
		$filter = new ParamFilter($params);
		//调用业务逻辑
		$service = App::bean('\\app\\service\\admin\\UserService');
		$table = $service->table()->select('id','login_name','is_admin','email','qq','create_time','edit_time');
		$service->queryByFilter($filter,$table);
		$this->json($filter->grid);
	}
	
	//加载新增用户面板
	function getAdd(){
		return $this->view("admin.user_add");
	}
	//保存修改用户
	function postAdd($params){
		$info = new JsonInfo();
		if($params['is_admin']){
			$params['is_admin'] = 1;
		}else{
			$params['is_admin'] = 0;
		}
		if(empty($params['login_name'])){
			$info->msg = "非法数据";
		}else{
			$service = App::bean('\\app\\service\\admin\\UserService');
			if(!empty($params['login_pwd'])){
				$params['login_pwd'] = md5($params['login_pwd']);
			}
			$service->saveUser($params,$info);
		}
		return $this->json($info);
	}
	
	
	//删除用户
	function postDelete($params){
		$info = new JsonInfo();
		if(empty($params['Q_id_in'])){
			$info->msg = "非法数据";
		}else{
			$service = App::bean('\\app\\service\\admin\\UserService');
			$service->deleteById($params['Q_id_in'],$info);
		}
		return $this->json($info);
	}
	
	function getLogin($params,$req,$res){
		$session = $req->getSession();
		if($session['user']==null){
			return $this->view("admin.user_login");
		}else{
			$res->redirect('/admin/home/index');
		}
	}
	
	function postLogin($params,$req){
		$info = new JsonInfo();
		$loginName = $params['login_name'];
		$loginPwd = $params['login_pwd'];
		$loginPwd = strlen($loginPwd)==32?$loginPwd:md5($params['login_pwd']);
		$service = App::bean('\\app\\service\\admin\\UserService');
		$user = $service->getFirst('login_name',$loginName);
		if($user){
			if( $user->login_pwd==$loginPwd){
				$info->success=true;
				$info->msg='登录成功，即将进入.';
				$info->data = [];
				$info->data['type'] = $user->is_admin;
				
				//查找没有的权限，管理员具备所有权限
				$urls = '';
				$keys = '';
				if($user->is_admin==0){
					$menus = $service->findUserNoMenusByUserId($user->id);
					foreach($menus as $i=>$r){
						if(!empty($r->url)&&
							strpos($urls,','.$r->url.',')===false){
							$urls.=$r->url.',';
						}
						$keys.=$r->key.',';
					}
				}
				$user->urls = ','.$urls;
				$user->keys = ','.$keys;
				unset($menus);
				unset($urls);
				unset($keys);
				App::session('user',$user);	
				
				//记住密码
				global $APP_CONFIG;
				if($APP_CONFIG['REMEMBER_PWD']){
					setcookie("login_name", $user->login_name, time()+3600*24*365,"/");  
					setcookie("login_pwd", $loginPwd, time()+3600*24*365,"/");  
				}
				
				
			}else{
				$info->msg='输入的密码错误，请重新输入.';
			}
		}else{
			$info->msg = "输入的用户名不存在，请重新输入.";
		}
		if($params['autoLogin']) return;
		return $this->json($info);
	}
	
	function getLogout($params,$req,$res){
		setcookie("login_name",null,time() - 3600,"/");  
		setcookie("login_pwd",null,time() - 3600,"/");  
		App::session('user',null);
		session_unset();
		$referer = $req->getReferer();
		if(empty($referer)) $referer = '/admin/user/login';
		$res->redirect($referer );
	}
}